package com.nbgroup.service;

import com.nbgroup.dao.ExhibitionRepositoty;
import com.nbgroup.entity.Exhibition;
import com.nbgroup.entity.Ichnography;
import com.nbgroup.utils.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class ExhibitionService {

    @Autowired
    private ExhibitionRepositoty exhibitionRepositoty;
    @Autowired
    private QueryUtil queryUtil;

    public List<Exhibition> findExhibitionByPlatform(Integer icId){
        return  this.exhibitionRepositoty.findExhibitionByPlatform(icId);
    }


    public Exhibition findExhibitionById(String exId){
        return  this.exhibitionRepositoty.findOne(Integer.parseInt(exId));
    }
    /**
     * 查询 指定平面图 的展厅
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public List<Exhibition> findAllForIchnography(Integer ichnographyId){
        String sql = "SELECT * FROM `exhibition` WHERE ichnography_id = '"+ichnographyId+"' AND sts = '1'";
        List<Exhibition> exhibitionList = queryUtil.query(sql, Exhibition.class);
        return exhibitionList;
    }

    /**
     * 查询 指定展厅
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public List<Exhibition> findAllForExhibition(Integer exhibitionId){
        String sql = "SELECT * FROM `exhibition` WHERE exhibition_id = '"+exhibitionId+"' AND sts = '1'";
        List<Exhibition> exhibitionList = queryUtil.query(sql, Exhibition.class);
        return exhibitionList;
    }

    /**
     * 展厅保存
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int saveExhibition(Exhibition exhibition){
        try{
            exhibition.setSts("1");
            exhibition.setPlatformId(1);
            exhibitionRepositoty.save(exhibition);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展厅修改
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int updateExhibition(Exhibition exhibition){
        try{
            Exhibition exhibition1 = this.findOneForExhibition(exhibition.getExhibitionId());
            exhibition1.setExhibitionName(exhibition.getExhibitionName());
            exhibition1.setExhibitionContent(exhibition.getExhibitionContent());
            exhibitionRepositoty.saveAndFlush(exhibition1);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }

    /**
     * 展厅findOne
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public Exhibition findOneForExhibition(Integer exhibitionId){
        String sql = "SELECT * FROM `exhibition` WHERE exhibition_id = '"+exhibitionId+"'";
        List<Exhibition> exhibitionList = queryUtil.query(sql, Exhibition.class);
        if(exhibitionList != null){
            return exhibitionList.get(0);
        }else{
            return null;
        }
    }

    /**
     * 展厅删除--逻辑
     * 2018年8月16日, AM 10:56:53
     *
     * @author 姜文
     */
    public int deleteExhibition(Integer exhibitionId){
        try{
            Exhibition exhibition = this.findOneForExhibition(exhibitionId);
            exhibition.setPlatformId(1);
            exhibition.setSts("0");
            exhibitionRepositoty.saveAndFlush(exhibition);
            return 1;
        }catch(Exception e){
            return -1;
        }
    }
}
